/**
 * 自定义TinyMCE配置
 */
function initTiny(tinyId){
    initTiny(tinyId, "", "");
}

function initTiny(tinyId, content, url){
	console.log(content + url);
    var sendUrl = 'common/fileupload/uploadimage';
    if(url!="" && url != undefined){
        sendUrl = url + sendUrl;
    }
    tinymce.init({
        selector: '#'+tinyId,
        language:'zh_CN',
        placeholder: '在这里输入文字',
        plugins: 'print preview paste searchreplace autolink autosave save directionality code visualblocks visualchars fullscreen image link charmap hr pagebreak nonbreaking anchor toc insertdatetime advlist lists wordcount imagetools textpattern noneditable table charmap quickbars emoticons autoresize indent2em lineheight axupimgs bdmap',
        menubar: 'file edit view insert format tools table',
        toolbar:'undo redo |bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | indent2em lineheight numlist bullist | link image axupimgs bdmap|forecolor backcolor removeformat subscript superscript',
        branding: false,
        custom_undo_redo_levels: 10,
        images_upload_handler: function (blobInfo, succFun, failFun) {
            var xhr, formData;
            var file = blobInfo.blob();
            xhr = new XMLHttpRequest();
            xhr.withCredentials = false;
            xhr.open('POST', sendUrl);
            xhr.onload = function() {
                var json;
                if (xhr.status != 200) {
                    failFun('HTTP Error: ' + xhr.status);
                    return;
                }
                json = JSON.parse(xhr.responseText);
                if (!json || json.code != "0") {
                    failFun(json.msg);
                    return;
                }
                succFun(json.result.filePath);
            };
            formData = new FormData();
            formData.append('file', file, file.name );
            xhr.send(formData);
        },
        file_picker_callback: function (callback, value, meta) {
            //文件分类
            var filetype='';
            //后端接收上传文件的地址
            var upurl='';
            //为不同插件指定文件类型及后端地址
            switch(meta.filetype){
                case 'image':
                    filetype='.jpg, .jpeg, .png, .gif';
                    upurl='../../common/aliyun/uploadimage';
                    break;
                case 'file':
                    filetype='.pdf, .txt, .zip, .rar, .doc, .docx, .xls, .xlsx, .ppt, .pptx';
                    upurl='../../common/aliyun/uploadfile';
                    break;
                default:
            }
            //模拟出一个input用于添加本地文件
            var input = document.createElement('input');
                input.setAttribute('type', 'file');
                input.setAttribute('accept', filetype);
            input.click();
            input.onchange = function() {
                var file = this.files[0];
    
                var xhr, formData;
                console.log(file.name);
                xhr = new XMLHttpRequest();
                xhr.withCredentials = false;
                xhr.open('POST', upurl);
                xhr.onload = function() {
                    var json;
                    if (xhr.status != 200) {
                        alert('HTTP Error: ' + xhr.status);
                        return;
                    }
                    json = JSON.parse(xhr.responseText);
                    console.log(json);
                    if (!json || json.code != "0") {
                        alert(json.msg);
                        return;
                    }
                    callback(json.result.url);
                };
                formData = new FormData();
                formData.append('file', file, file.name );
                xhr.send(formData);
            }
        },
    });
    if(content!="" && content != undefined){
        tinymce.get(tinyId).setContent(content);
    }
}